/*-+-*****************************************************************************
 Copyright (C) 2009-2012 Oceanside Software Corporation (R)
 Dallas, Texas

 Author       : Jason T. Brower
 Purpose      : This file is part of a point-of-sale software package
                originally written for Table-Service Restaurants.
              
 Date         : July 8th of 2012
              
 History      : The application that this software file belongs to was 
                written over a 3-Year period.  The first version was written
                in C# .Net 3.5 using Windows Forms.  Being new to .Net at
                the time, I soon learned of Windows Presentation Foundation,
                liked the graphics capabilities and databinding so I
                migrated to WPF.  When the .Net 4.0 framework was released
                the application was migrated from 3.5 to 4.0. Now today,
                .Net 4.5 RC is out and I was already in process of migrating
                before I made this public.
                
Excuse Me     : Please excuse the coding style, it is not representative of
                my typical work.  During the three years I was under a very
                tight budget, constantly running out of funding and was 
                working 18 hour days hyped up on 5-Hour Energy.  The 
                design is not textbook.  It is monolythic because of
                previous issues I will not get into.  There are few comments
                in the code and the ones that are there may be incorrect.
                
What it is not: This is not the exact version that I took through PA-DSS 2.0
                nor is it the exact version running at my customer sites.
                I removed some details for security purposes including some
                encryption algorithms.  The reporting was removed because
                I was including a third party library that cannot be 
                published with this Open Source.
                
What it is    : This application has been running at high volume restaraunts
                for over a year.  It never freezes, crashes or does anything
                unexpected.  So long as it is built, installed and configured
                correctly, it is a strong system that we can all enhance.

Contact       : Please note that I can no longer work for free.  However, you
                can reach me at information@oceansidesw.com.                                                      

*********************************************************************************/
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
#if _LOCALDB
  using Oceanside.LocalDB;
#else
using Oceanside.RemoteDB;
#endif

namespace Oceanside
{
    public class Helpers
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param Name="str"></param>
        /// <param Name="num"></param>
        public static int PaddWithChar(StringBuilder Receipt, int num, char c)
        {
            int count = 0;
            if (num >= 1)
            {
                for (int i = 0; i < num; i++)
                {
                    count++;
                    Receipt.Append(c);
                }

                if (c == '-')
                {
                    Receipt.Append("\n");
                }
            }
            return count;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <param name="num"></param>
        /// <param name="c"></param>
        public static void PaddWithChar(ref string str, int num, char c)
        {
            if (num >= 1)
            {
                for (int i = 0; i < num; i++)
                {
                    str += c;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param Name="str"></param>
        /// <param Name="num"></param>
        public static void AddEmpSignatureLine(StringBuilder Receipt, int recLineChars)
        {
            Receipt.Append(PrintConst.EMPSIG);
            PaddWithChar(Receipt, recLineChars - PrintConst.EMPSIG.Length, '_');
            Receipt.Append(Environment.NewLine);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param Name="str"></param>
        /// <param Name="num"></param>
        public static void AddManagerSignatureLine(StringBuilder Receipt, int recLineChars)
        {
            Receipt.Append(PrintConst.MANSIG);
            PaddWithChar(Receipt, recLineChars - PrintConst.MANSIG.Length, '_');
            Receipt.Append(Environment.NewLine);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param Name="str"></param>
        /// <param Name="num"></param>
        public static void AddCustomerSignatureLine(StringBuilder Receipt, int recLineChars)
        {
            Receipt.Append(PrintConst.CUSTSIG);
            PaddWithChar(Receipt, recLineChars - PrintConst.CUSTSIG.Length, '_');
            Receipt.Append(Environment.NewLine);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sb"></param>
        public static void Init(StringBuilder sb)
        {
            sb.Append(PrintConst.INIT);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sb"></param>
        public static void AppendHeader(StringBuilder sb,
            ORDER order,
            DateTime? dateTime = null,
            bool addCustHeader = true,
            string seatNoString = null)
        {
            sb.Append(PrintConst.BLACK + PrintConst.LEFT + PrintConst.BOLD);

            DateTime nonNullDateTime;
            if (addCustHeader)
            {
                AddCustomerHeader(sb);
            }

            if (dateTime == null)
            {
                nonNullDateTime = DateTime.Now;
            }
            else
            {
                nonNullDateTime = (DateTime)dateTime;
            }
            sb.Append(nonNullDateTime.ToString("MM/dd/yy", CultureInfo.CreateSpecificCulture("en-US")));
            sb.Append(" Time: " + nonNullDateTime.ToString("h:mm tt", CultureInfo.CreateSpecificCulture("en-US")) + PrintConst.NL);

            if (order != null)
            {
                sb.Append("Employee:" + USER.GetUserById(order.UserId).ScreenName + PrintConst.NL);
                sb.Append(KIOSK.GetKioskNameForId((long)order.KioskWhereCreatedId) + PrintConst.NL);
                sb.Append(PrintConst.NL + "OrderId: " + order.Id + PrintConst.NL + PrintConst.NL);

                Helpers.PaddWithChar(sb, PrintConst.MAX_LINE_WIDTH, '-');
                sb.Append(order.TableString);
                if (seatNoString != null)
                {
                    sb.Append(" - Seat #" + seatNoString);
                }
                sb.Append(PrintConst.NL);

                if (order.HasHeadOfTable)
                {
                    sb.Append("Customer:" + order.CustomerName + PrintConst.NL);
                }
            }

            Helpers.PaddWithChar(sb, PrintConst.MAX_LINE_WIDTH, '-');
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sb"></param>
        public static void AppendPayHeader(StringBuilder sb,
            ORDER order,
            string checknostring = null)
        {
            sb.Append(PrintConst.BLACK + PrintConst.LEFT + PrintConst.BOLD);
            DateTime nonNullDateTime;
            AddCustomerHeader(sb);
            nonNullDateTime = DateTime.Now;
            sb.Append(nonNullDateTime.ToString("MM/dd/yy", CultureInfo.CreateSpecificCulture("en-US")));
            sb.Append(" Time: " + nonNullDateTime.ToString("h:mm tt", CultureInfo.CreateSpecificCulture("en-US")) + PrintConst.NL);

            if (order != null)
            {
                sb.Append("Employee:" + USER.GetUserById(order.UserId).ScreenName + PrintConst.NL);
                sb.Append(KIOSK.GetKioskNameForId((long)order.KioskWhereCreatedId) + PrintConst.NL);
                sb.Append(PrintConst.NL + "OrderId: " + order.Id + PrintConst.NL + PrintConst.NL);

                Helpers.PaddWithChar(sb, PrintConst.MAX_LINE_WIDTH, '-');
                sb.Append(order.TableString);
                if (checknostring != null)
                {
                    sb.Append(" - Check #" + checknostring);
                }
                sb.Append(PrintConst.NL);

                if (order.HasHeadOfTable)
                {
                    sb.Append("Customer:" + order.CustomerName + PrintConst.NL);
                }
            }

            Helpers.PaddWithChar(sb, PrintConst.MAX_LINE_WIDTH, '-');
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="po"></param>
        /// <param name="list"></param>
        public static void AddPrinterGrouping(Int64 printerGroupingId, List<PrintObject> list)
        {
            bool doesContain = false;
            foreach (PrintObject listObj in list)
            {
                if (listObj.PrinterGroupingId == printerGroupingId)
                {
                    doesContain = true;
                    break;
                }
            }
            if (!doesContain)
            {
                list.Add(new PrintObject(printerGroupingId));
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="recpStr"></param>
        public static void AddCustomerHeader(StringBuilder Receipt)
        {
            SYSCONFIG syscfg = SYSCONFIG.Get(SHIFT.GlobalContext);
            Receipt.Append(PrintConst.CENTER);

            if (!string.IsNullOrEmpty(syscfg.ReceiptLine1))
            {
                Receipt.AppendLine();
                Receipt.AppendLine(syscfg.ReceiptLine1);
            }
            if (!string.IsNullOrEmpty(syscfg.ReceiptLine2))
            {
                Receipt.AppendLine(syscfg.ReceiptLine2);
            }
            if (!string.IsNullOrEmpty(syscfg.ReceiptLine3))
            {
                Receipt.AppendLine(syscfg.ReceiptLine3);
            }
            if (!string.IsNullOrEmpty(syscfg.ReceiptLine4))
            {
                Receipt.AppendLine(syscfg.ReceiptLine4);
            }
            
            Receipt.Append(PrintConst.LEFT);

            if (!string.IsNullOrEmpty(syscfg.TopReceiptMessage))
            {
                Receipt.AppendLine(Environment.NewLine + syscfg.TopReceiptMessage);
            }

            Receipt.AppendLine();
        }

        public static void AddCustomerFooter(StringBuilder sb)
        {
            SYSCONFIG syscfg = SYSCONFIG.Get(SHIFT.GlobalContext);
            if (!string.IsNullOrEmpty(syscfg.TopReceiptMessage))
            {
                sb.Append(PrintConst.CENTER);
                sb.AppendLine(Environment.NewLine);
                sb.AppendLine(syscfg.BottomReceiptMessage);
                sb.Append(PrintConst.LEFT);
            }
        }
    }
}
